// components/authPhoneNumber/authPhoneNumber.js
let request = require('../../utils/request.js')

Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    authorizationPhone: {
      type: Boolean,
      value: false
    },
    code: {
      type: String,
      value:'',
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
      //关闭授权手机号
  authorizationPhoneVanBind() {
    this.setData({
      authorizationPhone: false,
    });
  },
  //授权手机号
  getPhoneNumber(e) {
    var that = this;
    if(e.detail.errMsg == "getPhoneNumber:ok") {
      let encryptedData = e.detail.encryptedData;
      let iv = e.detail.iv;
      let url = "/customer/info/bindMobilePhone";
      wx.checkSession({
        success() {
          //session_key 未过期，并且在本生命周期一直有效（这里面可以不写代码）
        },
        fail() {
          // session_key 已经失效，需要重新执行登录流程并更新code
          wx.login({
            success: res => {
              code = res.code
              that.setData({ code })
            }
          })
        },
        complete() {
          //将code,encryptedData,iv传给后台进行解密
          let params = { code: that.data.code, encryptedData, iv, };
          request
          .http(url, "GET", params)
          .then((res) => {
            console.log(res.data.data)
            if (res.data.code == 200) {
              that.setData({
                authorizationPhone: false,
              });
              wx.setStorageSync('phoneNumber', res.data.data)
              wx.showToast({ title: '绑定成功', icon:'none' })
            }
          })
          .catch((err) => {
            console.error(err);
          });
        }
      })
    }
  },
  

  }
})
